home *** CD-ROM | disk | FTP | other *** search
- #include <MacTypes.h>
- #include <MacMemory.h>
- #include <Quickdraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <Menus.h>
- #include <MacWindows.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <OSUtils.h>
- #include <ToolUtils.h>
- #include <SegLoad.h>
- #include <Processes.h>
- #include <Movies.h>
- #include <QuickTimeComponents.h>
- #include <StandardFile.h>
- #include <NumberFormatting.h>
- #include <Files.h>
- #include <Endian.h>
-
-
- void Initialize(void);
- void DecompressSomething( FSSpec *inFile, FSSpec *outFile, short outScript );
- void DoDecompress(void);
-
-
- void main(void)
- {
- Initialize();
-
- DoDecompress();
- }
-
-
- void Initialize(void)
- {
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
- }
-
- void DecompressSomething( FSSpec *inFile, FSSpec *outFile, short outScript )
- {
- OSErr err = 0;
- DataDecompressorComponent ci = 0;
- short fref = 0;
- long uncompressedLength = 0;
- Handle uncompressedH = nil;
- long compressedLength = 0;
- Handle compressedH = nil;
- unsigned long ignoreThis = 0;
- long count;
-
- err = FSpOpenDF( inFile, fsRdPerm, &fref );
- if( err ) goto bail;
-
- err = GetEOF( fref, &compressedLength );
- if( err ) goto bail;
-
- // First four bytes are the size of the uncompressed data.
- count = 4;
- err = FSRead( fref, &count, &uncompressedLength );
- if( err ) goto bail;
- uncompressedLength = EndianU32_BtoN( uncompressedLength );
-
- compressedLength -= 4;
-
- compressedH = NewHandle( compressedLength );
- err = MemError();
- if( err ) goto bail;
- HLock( compressedH );
-
- uncompressedH = NewHandle( uncompressedLength );
- err = MemError();
- if( err ) goto bail;
- HLock( uncompressedH );
-
- count = compressedLength;
- err = FSRead( fref, &count, *compressedH );
- if( err ) goto bail;
-
- FSClose( fref );
- fref = 0;
-
- err = FSpDelete( outFile );
- if( err == fnfErr ) err = noErr;
- if( err ) goto bail;
-
- err = FSpCreate( outFile, 'CWIE', 'TEXT', outScript );
- if( err ) goto bail;
-
- err = FSpOpenDF( outFile, fsWrPerm, &fref );
- if( err ) goto bail;
-
- err = OpenADefaultComponent( DataDecompressorComponentType, zlibDataCompressorSubType, &ci );
- if( err ) goto bail;
-
- err = DataCodecDecompress( ci,
- *compressedH, compressedLength,
- *uncompressedH, uncompressedLength );
- if( err ) goto bail;
-
- count = uncompressedLength;
- err = FSWrite( fref, &count, *uncompressedH );
- if( err ) goto bail;
-
- bail:
- if( fref )
- FSClose( fref );
- if( ci )
- CloseComponent( ci );
- if( uncompressedH )
- DisposeHandle( uncompressedH );
- if( compressedH )
- DisposeHandle( compressedH );
-
- if( err )
- DebugStr( "\p oops." );
- }
-
-
- void DoDecompress(void)
- {
- StandardFileReply inreply, outreply;
- OSType list[1] = { 'ZOT ' };
-
- StandardGetFile( nil, 1, list, &inreply );
- if( !inreply.sfGood ) return;
-
- StandardPutFile( "\pSave decompressed file as:", "\puntitled", &outreply );
- if( !outreply.sfGood ) return;
-
- DecompressSomething( &inreply.sfFile, &outreply.sfFile, outreply.sfScript );
- }
-